<template>
  <div class="Housekeeping">
    <van-list v-model="loading" :finished="finished" @load="onLoad" :error.sync="err" error-text="加载失败，点击这里重试">
      <van-tabs v-model="active" @click="onClick" class="topNav">

        <van-tab title="预约订单">
          <div class="Box" @click="jump(item)" v-for="(item,index) in list" :key="index">
            <div class="Box_top">
              <div class="image">
                <van-image :src="`${resourceURL}${item.top_pic}`" />
              </div>
              <div class="right">
                <div class="top">
                  <div class="name">{{item.service_name}}</div>
                  <div class="status" :style="{color:item.state==4?'rgba(32,35,39,0.7)':''}">{{ status[item.state] }} </div>

                </div>
                <div class="center">{{formatCreateTime(new Date(item.make_time).getTime())}}</div>
              </div>
            </div>
            <div class="Box_bottom">
              <div class="price"><span style="font-weight:400">已付订金</span> ￥{{item.pay_amount}}</div>
              <van-button type="primary" round v-if="item.state==1" @click.stop="cancal(item)">取消预订</van-button>

            </div>
          </div>

          <van-empty description="暂无数据" v-if="list.length==0" />

        </van-tab>
        <van-tab title="已完成">
          <div class="Box" @click="jump(item)" v-for="(item,index) in list" :key="index">
            <div class="Box_top">
              <div class="image">
                <van-image :src="`${resourceURL}${item.top_pic}`" />
              </div>
              <div class="right">
                <div class="top">
                  <div class="name">{{item.service_name}}</div>

                  <div :class="item.state==4?'failStatus':'status'">{{ status[item.state] }}</div>
                </div>
                <div class="center">{{formatCreateTime( new Date(item.make_time).getTime() )}}</div>
              </div>
            </div>
            <div class="Box_bottom">
              <div class="price"><span style="font-weight:400">已付订金</span> ￥{{item.pay_amount}}</div>
              <van-button type="primary" round @click.stop="jumpToCommont(item)" v-if="item.state==3&&item.is_comment==0">评价</van-button>
            </div>
          </div>
          <van-empty description="暂无数据" v-if="list.length==0" />
        </van-tab>

      </van-tabs>
    </van-list>
  </div>
</template>
<script>
import { Toast, Dialog } from "vant";
export default {
  data () {
    return {
      loading: false,
      finished: true,
      list: [],
      page: 1,
      active: this.$route.query.sign || "0",
      searchValue: "",
      err: false,
      status: {
        1: "未派单",
        2: "已派单",
        3: "已完成",
        4: "取消预约",
        5: "已退款",
      },
    };
  },
  methods: {
    //加载下一页
    onLoad () {
      this.serviceOrderlist();
    },
    //时间转化
    formatCreateTime (val) {
      return this.$moment(val).calendar(null, {
        sameDay: "MM月DD日 [今天] HH:mm:ss",
        nextDay: "MM月DD日 [明天] HH:mm:ss",
        nextWeek: "MM月DD日  HH:mm:ss",
        lastDay: "MM月DD日 [昨天] HH:mm:ss",
        lastWeek: "MM月DD日  HH:mm:ss",
        sameElse: 'MM月DD日  HH:mm:ss'
      });
    },
    cancal (item) {
      // this.result.push(item.product_id)

      Dialog.confirm({
        confirmButtonColor: "#ff7733",
        cancelButtonColor: "rgba(32,35,39,0.5)",
        message: "确认取消预订吗？",
      })
        .then(() => {
          this.cancelCollect(item.send_id);
        })
        .catch(() => {
          // on cancel

        });
    },
    cancelCollect (send_id) {
      //如果没有code 去获取code

      this.$axios
        .post(this.$api.serviceafter, {
          send_id: send_id,
        })
        .then((res) => {

          if (res.data.code == 1) {
            Toast.success(res.data.msg);
            this.page = 1
            this.serviceOrderlist()
          } else {
            Toast.fail(res.data.msg);
          }
        });
    },
    //跳转评论页
    jumpToCommont (item) {
      this.$router.push({
        name: 'addComment', query: {
          order_id: item.order_id,
          send_id: item.send_id,
          type: 'Housekeeping'
        }
      })
    },
    //获取家政订单
    serviceOrderlist () {
      this.loading = true;

      this.$axios
        .get(this.$api.serviceOrderlist, {
          params: {
            page: this.page,
            limit: 6,
            is_page: 2,
            state: Number(this.active) + 1,
          },
        })
        .then((res) => {
          this.loading = false;
          if (res.data.code == 1) {
            if (res.data.data.length < 6) {
              this.finished = true;
            } else {

              this.finished = false;
            }
            this.list = [...this.list, ...res.data.data];

          } else {
            this.err = true;
            this.loading = false;
          }
        });
    },

    onClick () {


      this.page = 1;
      this.list = [];
      this.serviceOrderlist();
    },

    jump (item) {
      this.$router.push({
        name: "HouseOrderDetail",
        query: {
          send_id: item.send_id,
        },
      });
    },
  },
  watch: {},
  created () {
    this.serviceOrderlist();
  },
  mounted () { },
  computed: {},
};
</script>
<style scoped lang="less">
/deep/ .van-tabs__nav {
  background: #fff;
}
.Housekeeping {
  //background: #fff;
  min-height: 100vh;
}

.topNav {
  /deep/.van-tab__text {
    font-size: 14px;
    font-weight: 400;
    font-weight: bold;
  }
  /deep/.van-tab--active {
    color: #ff7733;
  }
  /deep/.van-tabs__line {
    width: 28px;
    height: 2px;
    background: #ff7733;
  }
  /deep/ .van-tabs__wrap {
    background: white;
    padding: 4.5px 15px;
    box-sizing: border-box;
  }
}
.Box {
  width: 343px;
  height: 152px;
  background: #ffffff;
  border-radius: 10px 10px 10px 10px;
  opacity: 1;
  border: 1px solid #f2f2f2;
  padding: 16px;
  box-sizing: border-box;
  margin: 0 auto;
  margin-top: 14px;
  /deep/ .van-image {
    width: 100px;
    height: 80px;
  }
  .Box_top {
    display: flex;
    .right {
      flex: 1;
      margin-left: 12px;
      .top {
        display: flex;
        justify-content: space-between;
        font-family: Microsoft YaHei-Bold, Microsoft YaHei;
        .name {
          font-size: 16px;

          font-weight: bold;
          color: #06021a;
        }
        .status {
          font-size: 14px;
          color: #ff7733;
        }
        .failStatus {
          font-size: 14px;
          color: rgba(32, 35, 39, 0.7);
        }
      }
      .center {
        font-size: 14px;
        margin-top: 12px;
        font-weight: 400;
        color: #303030;
      }
    }
  }
  .Box_bottom {
    height: 32px;
    margin-top: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .price {
      font-size: 14px;
      font-family: Microsoft YaHei-Bold, Microsoft YaHei;
      font-weight: bold;
      color: #06021a;
    }

    button {
      width: 80px;
      height: 32px;
      background: #ffffff;
      color: #ff7733;
      opacity: 1;
      border: 1px solid #ff7733;
      white-space: nowrap;
      font-size: 12px;
      font-family: Microsoft YaHei-Regular, Microsoft YaHei;
      font-weight: 400;
      color: #ff7733;
      text-align: center;
    }
  }
}
</style>
